package
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	
	/**
	 * ...
	 * @author 
	 */
	import flash.events.*;
	
	public class Crab extends MovieClip
	{
		
		var Angle:int = 90;
		
		var MC_Leg:DisplayObject;
		
		private var leftPressed:Boolean = false;
		private var rightPressed:Boolean = false;
		private var upPressed:Boolean = false;
		private var downPressed:Boolean = false;
		private var spacePressed:Boolean = false;
		
		
		public const ANGLE_SPEED:int = 5;
		
		public function Crab() 
		{
			
			
			this.addEventListener(Event.ADDED_TO_STAGE, onATS);
		}
		
		private function onATS(e:Event) {
			
			this.removeEventListener(Event.ADDED_TO_STAGE, onATS);
			
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			
			
			MC_Leg = getChildByName("Leg");
			
		}
		public function frameH(e:Event):void {
			
			
			MC_Leg.rotation = Angle;
			
			
			
			if (rightPressed) {
				this.x += 5;
			}
			if (leftPressed) {
				this.x -= 5;
			}
			if (upPressed) {
				Angle += ANGLE_SPEED;
			}
			if (downPressed) {
				Angle -= ANGLE_SPEED;
			}
			if (spacePressed) {
				dispatchEvent(new CustomEvent(CustomEvent.SHOOT_BUBBLE));
			}
			
			
		}
		
		private function onKeyDown(e:KeyboardEvent):void {
			switch(e.keyCode) {
				case 37:	//Left
				leftPressed = true;
				break;
				case 39:	//Right
				rightPressed = true
				break;
				case 38:	//Up
				upPressed = true;
				break;
				case 40:	//Down
				downPressed = true
				break;
				case 32:	//space
				spacePressed = true;
				break;
			}
		}
		private function onKeyUp(e:KeyboardEvent):void {
			switch(e.keyCode) {
				case 37:	//Left
				leftPressed = false;
				break;
				case 39:	//Right
				rightPressed = false
				break;
				case 38:	//Up
				upPressed = false;
				break;
				case 40:	//Down
				downPressed = false
				break;
				case 32:	//space
				spacePressed = false;
				break;
			}
		}
		
	}

}